package com.qf.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.qf.entity.Parking;
import com.qf.entity.Waterfee;
import com.qf.result.ResponseResult;
import com.qf.service.ParkingService;
import com.qf.utils.MailUtil;
import com.qf.utils.PayUtil;
import com.qf.utils.Utils;
import com.qf.vo.ChartVo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/parking")
@Slf4j
public class ParkingController {

    @Autowired
    MailUtil mailUtil;

    @Autowired
    PayUtil payUtil;

    @Autowired
    ParkingService parkingService;

    @PostMapping("/pay")
    public ResponseResult pay(@RequestBody Parking parking, HttpServletRequest request, HttpServletResponse response){
        Double parkFee = parking.getParkFee();
        try {
            String alipay = payUtil.alipay(request, response, parkFee,parking.getId());

            return ResponseResult.okResult(alipay);
        }catch (Exception e){
            return ResponseResult.errorResult(201,"支付失败");
        }
//        return ResultBean.FAIL();
    }

    @PostMapping("/notify")
    public void notifyUrl(HttpServletRequest request) throws AlipayApiException {

        //先验签,
        Map<String, String> paramsMap = Utils.convertRequestParamsToMap(request); //将异步通知中收到的所有参数都存放到 map 中

        //
        log.info(paramsMap + "====================");

        boolean signVerified = AlipaySignature.rsaCheckV1(paramsMap, payUtil.getAPP_PRIVATE_KEY(), "utf-8", "RSA2");  //调用SDK验证签名
        //验签成功 再根据支付修改订单状态
        if (signVerified) {
            // TODO 验签成功后，按照支付结果异步通知中的描述，对支付结果中的业务内容进行二次校验，校验成功后在response中返回success并继续商家自身业务处理，校验失败返回failure
            //购买成功,修改数据库
//            UpdateWrapper<User> wrapper = new UpdateWrapper<>();
//            wrapper.set("account",)


        } else {
            // TODO 验签失败则记录异常日志，并在response中返回failure.
        }
    }

    @GetMapping("/findAll")
    public ResponseResult findAll(String ownerName){
        return parkingService.findAll(ownerName);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable Integer id){
        return parkingService.delete(id);
    }

    @PostMapping("/add")
    public ResponseResult add(@RequestBody @Valid Parking parking){
        parking.setCreateTime(new Date());
        parking.setFeeStatus("0");
        return parkingService.add(parking);
    }

    @GetMapping("/findAllParkNum")
    public ResponseResult findAllParkNum(){
        return parkingService.findAllParkNum();
    }

    @GetMapping("/waterfee")
    public ResponseResult chart(){
        ChartVo[] chartVos={new ChartVo("一月",70),new ChartVo("二月",80),new ChartVo("三月",75),new ChartVo("四月",100),new ChartVo("五月",103),new ChartVo("六月",93)};
        return ResponseResult.okResult(chartVos);
    }

    @GetMapping("/elefee")
    public ResponseResult waterfee(){
        ChartVo[] chartVos={new ChartVo("一月",230),new ChartVo("二月",301),new ChartVo("三月",250),new ChartVo("四月",360),new ChartVo("五月",300),new ChartVo("六月",220)};
        return ResponseResult.okResult(chartVos);
    }

    @PostMapping("/selectOwnerByParkId")
    public ResponseResult selectOwnerByParkId(Integer id){
        System.out.println("============"+id);
        return parkingService.selectOwnerByParkId(id);
    }

    @GetMapping("/chartCount")
    public ResponseResult chartCount(){
        String[][] strings = new String[][]{
                {"product", "一月", "二月", "三月", "四月", "五月", "六月"},
                {"车辆管理", "60", "89", "110", "140", "137", "160"},
                {"宠物数量", "51", "57", "55", "53", "73", "68"},
                {"租客数量", "40", "62", "69", "36", "45", "32"},
                {"管理人员", "20", "31", "11", "27", "36", "49"}
        };
        return ResponseResult.okResult(strings);
    }
}
